package com.ftrend.printdemo.common

import android.content.Context
import android.util.Log
import com.loopstack.gprint.utils.LabelCommand
import java.io.IOException

class PrintBarCodeLable {
    lateinit var mContext: Context
    companion object{
        const val PRINT_CHINESE_WIFTH = 24
        const val PRINT_CHAR_WIDTH = 12
        const val PRINT_CHAR_WIDTH_SMALL = 12
        const val PRINT_CHAR_HEIGH_SMALL_30 = 6
        const val ptintWidth = 40
        const val ptintHeigh = 60
        const val smallLine = false
        const val unit="￥"
    }
    fun sendLabel():Unit{
        Log.e("PrinterBlue","-1")
//        if (!PrinterBlue.isBarConnect() && !PrinterBarUsb.isBarConnect()) {
//            return
//        }
        var yPoint = 0
        var xPoint = 1

        val tsc = com.loopstack.gprint.utils.LabelCommand()
        /* 设置标签尺寸，按照实际尺寸设置 */
        tsc.addSize(ptintWidth, ptintHeigh)
        /* 设置标签间隙，按照实际尺寸设置，如果为无间隙纸则设置为0 */
        tsc.addGap(2)
        /* 设置打印方向 */
        tsc.addDirection(com.loopstack.gprint.utils.LabelCommand.DIRECTION.FORWARD, com.loopstack.gprint.utils.LabelCommand.MIRROR.NORMAL)
        /* 开启带Response的打印，用于连续打印 */
        tsc.addQueryPrinterStatus(com.loopstack.gprint.utils.LabelCommand.RESPONSE_MODE.ON)
        /* 设置原点坐标 */
        tsc.addReference(0, 0)
        /* 撕纸模式开启 */
        tsc.addTear(com.loopstack.gprint.utils.LabelCommand.RESPONSE_MODE.ON)
        /* 清除打印缓冲区 */
        tsc.addCls();
        //tsc.addHome();
        tsc.addSpeed(com.loopstack.gprint.utils.LabelCommand.SPEED.SPEED12)
        tsc.addDensity(com.loopstack.gprint.utils.LabelCommand.DENSITY.DNESITY1);
        var x = PRINT_CHAR_WIDTH
        var y = PRINT_CHINESE_WIFTH * yPoint + 6* (yPoint - 1)+ PRINT_CHAR_WIDTH_SMALL + PRINT_CHAR_HEIGH_SMALL_30
        tsc.addText(x, PRINT_CHAR_HEIGH_SMALL_30, com.loopstack.gprint.utils.LabelCommand.FONTTYPE.SIMPLIFIED_24_CHINESE, com.loopstack.gprint.utils.LabelCommand.ROTATION.ROTATION_0, com.loopstack.gprint.utils.LabelCommand.FONTMUL.MUL_1, com.loopstack.gprint.utils.LabelCommand.FONTMUL.MUL_1,
        "测测打印")
        tsc.add1DBarcode(xPoint * PRINT_CHAR_WIDTH, y+6* (yPoint - 1)+ PRINT_CHAR_WIDTH_SMALL + PRINT_CHAR_HEIGH_SMALL_30, com.loopstack.gprint.utils.LabelCommand.BARCODETYPE.CODE128, 50, com.loopstack.gprint.utils.LabelCommand.READABEL.EANBEL, com.loopstack.gprint.utils.LabelCommand.ROTATION.ROTATION_0, "213676423");

        /* 打印标签 */
        tsc.addPrint(2)

        val datas = tsc.getCommand()
        Log.e("PrinterBlue","0")
        if (PrinterBlue.isBarConnect()) {
            try {
                Log.e("PrinterBlue","1")
                PrinterBlue.getPortManager().writeDataImmediately(datas)
            } catch (e: IOException) {
                e.printStackTrace();
            }
        }else if(PrinterBarUsb.isBarConnect()){
            try {
                PrinterBarUsb.getPortManager().writeDataImmediately(datas)
            } catch (e: IOException) {
                e.printStackTrace();
            }
        }
    }

}